// test4.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<stdint.h>
static int findInBuf(const char* buf,const int len,const char* needle);
const char ipaddr[]="006900700061006400640072003A";
const char message_txt[]="+CMGL: 2,\"REC READ\",\"+8613858088973\",\"\",\"2016/01/22 13:56:23+32\"\r\nipaddr:1,120.55.194.216,1819\r\n\r\nOK";
char tmp_buff[50];

int _tmain(int argc, _TCHAR* argv[])
{
	uint8_t p,p2;
	p=findInBuf(message_txt,sizeof(message_txt),"ipaddr:");
	sprintf(tmp_buff,"%s",(message_txt+p));
	p=findInBuf(tmp_buff,sizeof(tmp_buff),"\r\n");
	*(tmp_buff+p-2)=0;
	return 0;
}

static int findInBuf(const char* buf,const int len,const char* needle)
{
  int i;
  int needleMatchedPos=0;

  if (needle[0] == '\0') {
    return 1;
  }

  for (i=0;i<len;i++) {
    if (needle[needleMatchedPos] == buf[i]) {
      needleMatchedPos++;
      if (needle[needleMatchedPos] == '\0') {
	// Entire needle was found
	return (i+1);
      }
    } else {
      needleMatchedPos=0;
    }
  }
  return 0;
}
